<?php

namespace app\index\traits;

use app\common\model\Member;
use app\index\service\MemberDraftService;
use think\exception\ValidateException;

/**
 * Trait SessionMemberDraftTrait
 * @package app\index\traits
 */
trait SessionMemberDraftTrait
{
    //检查律师认证资料（资料是否完整）
    protected function checkMemberDraft(?array $memberDraft): void
    {
        if (!is_array($memberDraft) || empty($memberDraft)) {
            return;
        }
        if (is_blank($memberDraft['headImgUrl'] ?? null)) {
            throw new ValidateException('请上传头像');
        }
        if (is_blank($memberDraft['realname'] ?? null)) {
            throw new ValidateException('请输入真实姓名');
        }
        if (!is_id($memberDraft['cityId'] ?? null)) {
            throw new ValidateException('请选择一个城市');
        }
        if (is_blank($memberDraft['practiceNumber'] ?? null)) {
            throw new ValidateException('请输入执业编号');
        }
        if (is_blank($memberDraft['practicingLawFirm'] ?? null)) {
            throw new ValidateException('请输入执业律所');
        }
        if (is_blank($memberDraft['practiceTime'] ?? null)) {
            throw new ValidateException('请输入执业证书注册时间');
        }
        if (empty($memberDraft['categories'] ?? [])) {
            throw new ValidateException('请选择擅长领域');
        }
        if (is_blank($memberDraft['certificatePic'] ?? null)) {
            throw new ValidateException('请上传执业证原件');
        }
    }

    //保存律师认证资料
    protected function saveMemberDraft(Member $member, ?array $memberDraft, $pf, $chain, $promotionGroupId): void
    {
        if (!is_array($memberDraft) || empty($memberDraft)) {
            return;
        }

        $memberDraft['pf'] = $pf;
        $memberDraft['chain'] = $chain;
        $memberDraft['promotionGroupId'] = $promotionGroupId;

        $this->getMemberDraftService()->saveMemberDraft($member, $memberDraft);
    }

    private function getMemberDraftService(): MemberDraftService
    {
        return app(MemberDraftService::class);
    }

}